<template>
  <c-form-dialog
    ref="form"
    :form-data="editFormData"
    :form-items="formItems"
    :table-id="tableId"
    :table-name="tableName"
    :visible.sync="innerVisible"
    :before-save="handleBeforeSave"
    @dismiss="handleDismiss"
    @success="handleSuccess"
  ></c-form-dialog>
</template>
<script>
import { enumUnGuaType } from '@/credit/views/county/loan/component/un/index'
import { mapGetters } from 'vuex'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    queryId: {
      type: String,
      required: true,
    },
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      innerVisible: false,
      productName: '',
      editFormData: {
        un_gua_type: '',
        un_gua_time: '',
        un_gua_attachment: '',
      },
      formItems: [
        {
          tag: 'select',
          itemAttrs: {
            label: '解保类型',
            rules: [{ required: true, message: '请选择解保类型', trigger: 'change' }],
          },
          attrs: {
            key: 'un_gua_type',
            value: '',
            options: enumUnGuaType,
            placeholder: '请选择解保类型',
          },
        },
        {
          tag: 'date',
          itemAttrs: {
            label: '解保日期',
            rules: [{ required: true, message: '请选择解保日期', trigger: 'change' }],
          },
          appendSlot: 'appendSlot',
          attrs: { key: 'un_gua_time', value: '', placeholder: '请选择解保时间' },
        },
        {
          tag: 'upload',
          itemAttrs: {
            label: '解保附件',
            class: 'form-control-ml',
            // rules: [{ required: true, message: '请上传解保附件', trigger: 'change' }],
          },
          attrs: {
            key: 'un_gua_attachment',
            fileName: 0,
            value: '',
            placeholder: '解保附件',
            accept: '',
            listType: '',
            dir: `/credit/unload/${this.queryId}`,
          },
        },
      ],
      tableName: 'un_gua_apply',
    }
  },
  computed: {
    ...mapGetters(['user']),
  },
  watch: {
    formData: {
      handler: function () {
        this.initFormData()
      },
      deep: true,
      immediate: true,
    },
    visible(val) {
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleBeforeSave(model) {
      model.start_time = this.$utils.dateFormat(new Date())
      model.customer_related_id = this.queryId
      model.submit_person_name = this.user.realName
      model.un_gua_status = 1
      return model
    },
    handleDismiss() {
      this.innerVisible = false
      this.$emit('update:visible', false)
    },
    handleSuccess() {
      this.$emit('success', false)
      this.handleDismiss()
    },
    initFormData() {
      Object.keys(this.editFormData).forEach(key => {
        this.editFormData[key] = this.formData[key] || ''
      })
      this.$refs.form?.setAttrs('un_gua_attachment', { dir: `/credit/unload/${this.queryId}` })
    },
  },
}
</script>
<style lang="scss" scoped></style>
